const foodFacade = require('../../app/facade/food.facade.js'); 
const _  =  require('lodash');

module.exports = {
    addFood : addFood,
    deleteFood: deleteFood,
    updateFood: updateFood,  
    getFoodByFname: getFoodByFname, 
    getFoodByIcnum: getFoodByIcnum,
    getFoodByFtype: getFoodByFtype,
    getFoodByIcnumFtype: getFoodByIcnumFtype,
    getFoodById : getFoodById,
}

function addFood(req, res, next){
    let food = req.body;
    let files = req.files;
    console.log(food);
    console.log(files);
     foodFacade.addFood(food,files,(error,result)=>{
       res.redirect("/#!/foodlist");
               
     });
}

function deleteFood(req, res, next){
    let params = req.params;
    let _id = params._id;
    console.log(_id);
     foodFacade.deleteFood(_id,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }         
     });
}

function updateFood(req, res, next){
    let food = req.body;
    let files = req.files;
    console.log(food);
    console.log(files);
      foodFacade.updateFood(food,files,(error,result)=>{
         if (error) {
             return next(new Error(error));
         } else {
         res.redirect("/#!/foodlist");
         }         
      });
}

function getFoodByFname(req, res, next){
    let params = req.params;
    let fname = params.fname;
    foodFacade.getFoodByFname(fname,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });    

}

function getFoodByFtype(req, res, next){
    let params = req.params;
    let ftype = params.ftype;
    foodFacade.getFoodByFtype(ftype,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });
}

function getFoodByIcnum(req, res, next){
    let params = req.params;
    let icnum = params.icnum;
    foodFacade.getFoodByIcnum(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });
}

function getFoodByIcnumFtype(req, res, next){
    let body = req.body;
    let icnum = body.icnum;
    let ftype = body.ftype;
    foodFacade.getFoodByIcnumFtype(icnum,ftype,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }
    });

}

function getFoodById(req, res, next){
    let _id = req.params._id;
    foodFacade.getFoodById(_id,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            console.log(result);
            return res.json(result);
        }  
    });

}